package org.jeecg.modules.acmd.service.impl;

import org.jeecg.modules.acmd.entity.Acmd;
import org.jeecg.modules.acmd.mapper.AcmdMapper;
import org.jeecg.modules.acmd.service.IAcmdService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import static org.jeecg.modules.acmd.config.Config.adbRootPath;

/**
 * @Description: 批处理
 * @Author: jeecg-boot
 * @Date:   2022-08-25
 * @Version: V1.0
 */
@Service
public class AcmdServiceImpl extends ServiceImpl<AcmdMapper, Acmd> implements IAcmdService {
    @Override
    public String doCmd(String commandStr ) {
//
//        InputStream ins = null;
//
//        System.out.println("doCmd:" + commandStr);
//        String[] cmd = new String[]{"cmd.exe", "/C", adbRootPath + commandStr };  // 命令
//
////        try {
////            String cmd = "cmd /c start "+"D:/run cmd/a.bat".replaceAll(" ", "\" \"");
////            Runtime.getRuntime().exec(cmd) ;
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//
//
//        try {
//
//            Process process = Runtime.getRuntime().exec(cmd);
//
//            ins = process.getInputStream();  // 获取执行cmd命令后的信息
//
//            BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
//
//            String lineStr = "";
//            String line = null;
//
//            while ((line = reader.readLine()) != null) {
//                System.out.println(line);  // 输出
//                lineStr += line;
//            }
//
//            int exitValue = process.waitFor();
//
////            System.out.println("返回值：" + exitValue);
//
//            process.getOutputStream().close();  // 不要忘记了一定要关
//
//            return  lineStr;
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        }
        return "error out put";
    }

    @Override
    public String doPower(String commandStr ) {
        InputStream ins = null;

        System.out.println("doCmd:" + commandStr);
        String[] cmd = new String[]{"cmd.exe", "/C",   commandStr };  // 命令

//        try {
//            String cmd = "cmd /c start "+"D:/run cmd/a.bat".replaceAll(" ", "\" \"");
//            Runtime.getRuntime().exec(cmd) ;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        try {

            Process process = Runtime.getRuntime().exec(cmd);

            ins = process.getInputStream();  // 获取执行cmd命令后的信息

            BufferedReader reader = new BufferedReader(new InputStreamReader(ins));

            String lineStr = "";
            String line = null;

            while ((line = reader.readLine()) != null) {
                System.out.println(line);  // 输出
                lineStr += line;
            }

            int exitValue = process.waitFor();

//            System.out.println("返回值：" + exitValue);

            process.getOutputStream().close();  // 不要忘记了一定要关

            return  lineStr;
        } catch (Exception e) {

            e.printStackTrace();

        }
        return "error out put";
    }


}
